package com.jic.manager.impl;

import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.manager.common.util.BeanUtil;
import com.jic.manager.entity.MerchantWeBankContract;
import com.jic.manager.entity.MerchantWeBankContractFile;
import com.jic.manager.mapper.MerchantWeBankContractFileMapper;
import com.jic.manager.mapper.MerchantWeBankContractMapper;
import com.jic.manager.request.MerchantWeBankContractFileRequest;
import com.jic.manager.request.MerchantWeBankContractRequest;
import com.jic.manager.response.MerchantWeBankContractFileResponse;
import com.jic.manager.response.MerchantWeBankContractResponse;
import com.jic.manager.service.MerchantWeBankContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户合同信息表
 * @author : 
 * @email : 
 * @since : 2020-11-28 11:09:13
 * @version : v1.0.0
 */
@Service
public class MerchantWeBankContractServiceImpl implements MerchantWeBankContractService{

    @Autowired
    private MerchantWeBankContractMapper merchantWeBankContractMapper;

    @Autowired
    private MerchantWeBankContractFileMapper merchantWeBankContractFileMapper;

    @Override
    public long insert(MerchantWeBankContract merchantWeBankContract) {
        long flag = merchantWeBankContractMapper.insert(merchantWeBankContract);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return merchantWeBankContractMapper.delete(id);
    }

    @Override
    public long update(MerchantWeBankContract merchantWeBankContract) {
        return merchantWeBankContractMapper.update(merchantWeBankContract);
    }

    @Override
    public MerchantWeBankContract queryByPrimaryKey(Long id){
        return merchantWeBankContractMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MerchantWeBankContract> query(MerchantWeBankContract merchantWeBankContract){
        return merchantWeBankContractMapper.query(merchantWeBankContract);
    }

    @Override
    public PageResult<MerchantWeBankContractResponse> queryPage(MerchantWeBankContractRequest request, Page pageQuery) {
        MerchantWeBankContract merchantWeBankContract = BeanUtil.copy(request, MerchantWeBankContract.class);
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<MerchantWeBankContract> list = merchantWeBankContractMapper.queryPage(startIndex, pageQuery.getLimit(), merchantWeBankContract);
        List<MerchantWeBankContractResponse> listResponse = list.stream().map(temp -> {
            List<MerchantWeBankContractFileResponse>  contractFileList = merchantWeBankContractFileMapper.getListByContractId(new MerchantWeBankContractFile(){{
                setContractId(temp.getId());
            }});
            MerchantWeBankContractResponse contractResponse = BeanUtil.copy(temp, MerchantWeBankContractResponse.class);
            contractResponse.setContractFileList(contractFileList);
            return contractResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        long count = merchantWeBankContractMapper.queryPageCount(merchantWeBankContract);
        PageResult pageResult = new PageResult();
        pageResult.setRows(listResponse);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public long addContractInfo(MerchantWeBankContractRequest request) {
        AdminOperate adminOperate = request.getAdminOperate();
        MerchantWeBankContract merchantWeBankContract = BeanUtil.copy(request, MerchantWeBankContract.class);
        merchantWeBankContract.setCreator(adminOperate.getOperator());
        merchantWeBankContract.setCreatorName(adminOperate.getOperateName());
        merchantWeBankContract.setDeptId(adminOperate.getDeptId());
        long flag = merchantWeBankContractMapper.insert(merchantWeBankContract);
        List<MerchantWeBankContractFileRequest> contractFileList = request.getContractFileList();
        List<MerchantWeBankContractFileRequest> fileList = contractFileList.stream().map(temp -> {
            temp.setContractId(merchantWeBankContract.getId());
            return temp;
        }).filter(item -> item != null).collect(Collectors.toList());
        MerchantWeBankContractFileRequest fileRequest = new MerchantWeBankContractFileRequest();
        fileRequest.setContractId(merchantWeBankContract.getId());
        fileRequest.setUpdater(adminOperate.getOperator());
        fileRequest.setUpdaterName(adminOperate.getOperateName());
        fileRequest.setFileList(fileList);
        merchantWeBankContractFileMapper.updateContractId(fileRequest);
        return flag;
    }

    @Override
    public MerchantWeBankContractResponse getContractInfo(MerchantWeBankContractRequest request) {
        MerchantWeBankContract merchantWeBankContract = merchantWeBankContractMapper.getContractInfoByMerchantId(request);
        MerchantWeBankContractResponse response = BeanUtil.copy(merchantWeBankContract, MerchantWeBankContractResponse.class);
        List<MerchantWeBankContractFileResponse> list = merchantWeBankContractFileMapper.getListByContractId(new MerchantWeBankContractFile(){{
            setContractId(response.getId());
            setMerchantId(response.getMerchantId());
        }});
        response.setContractFileList(list);
        return response;
    }
}